package com.ityuwen.mengyagushi.order.service.template;

import com.ityuwen.mengyagushi.album.client.AlbumInfoFeignClient;
import com.ityuwen.mengyagushi.common.constant.SystemConstant;
import com.ityuwen.mengyagushi.common.result.Result;
import com.ityuwen.mengyagushi.common.util.AuthContextHolder;
import com.ityuwen.mengyagushi.model.album.AlbumInfo;
import com.ityuwen.mengyagushi.model.user.UserInfo;
import com.ityuwen.mengyagushi.user.client.UserInfoFeignClient;
import com.ityuwen.mengyagushi.vo.order.OrderDerateVo;
import com.ityuwen.mengyagushi.vo.order.OrderDetailVo;
import com.ityuwen.mengyagushi.vo.order.OrderInfoVo;
import com.ityuwen.mengyagushi.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@OrderType(OrderConfirmType.ORDER_CONFIRM_ALBUM)
public class AlbumOrderConfirm extends OrderConfirm {

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    public AlbumOrderConfirm(RedisTemplate redisTemplate) {
        super(redisTemplate);
    }

    @Override
    public void confirm(TradeVo tradeVo, OrderInfoVo orderInfoVo) {
        // 查询专辑的购买记录，判断是否已购买过。为了防止同一个专辑的重复购买
        Result<Boolean> paidAlbumResult = this.userInfoFeignClient.isPaidAlbum(tradeVo.getItemId());
        Assert.notNull(paidAlbumResult, "专辑订单确认页：获取专辑购买信息失败");
        Boolean isPaidAlbum = paidAlbumResult.getData();
        if (isPaidAlbum){
            throw new RuntimeException("该专辑已购买，请不要重复购买！");
        }

        // 查询专辑信息
        Result<AlbumInfo> albumInfoResult = this.albumInfoFeignClient.getAlbumInfo(tradeVo.getItemId());
        Assert.notNull(albumInfoResult, "专辑订单确认页：获取专辑信息失败！");
        AlbumInfo albumInfo = albumInfoResult.getData();
        Assert.notNull(albumInfo, "专辑订单确认页：获取专辑信息失败！");

        // 查询用户信息
        Result<UserInfo> userInfoResult = this.userInfoFeignClient.getUserInfo(AuthContextHolder.getUserId());
        Assert.notNull(userInfoResult, "专辑订单确认页：获取用户信息失败！");
        UserInfo userInfo = userInfoResult.getData();
        Assert.notNull(userInfo, "专辑订单确认页：获取用户信息失败！");

        // 判断用户的vip状态，获取不同的折扣信息
        BigDecimal discount = new BigDecimal(-1);
        if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())){
            discount = albumInfo.getVipDiscount();
        } else {
            discount = albumInfo.getDiscount();
        }
        // 如果是-1 则取10
        discount = discount.compareTo(new BigDecimal(-1)) == 0 ? new BigDecimal(10) : discount;

        // 计算三个金额
        BigDecimal originAmount = albumInfo.getPrice(); // 初始价格
        BigDecimal orderAmount = originAmount.multiply(discount).divide(new BigDecimal(10)); // 实付价格
        BigDecimal derateAmount = originAmount.subtract(orderAmount); // 减免金额
        orderInfoVo.setOriginalAmount(originAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);

        // 组装订单详情
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVoList.add(orderDetailVo);
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName(albumInfo.getAlbumTitle());
        orderDetailVo.setItemPrice(orderAmount);
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);

        // 订单减免信息：判断减免金额是否为0
        if (derateAmount.compareTo(new BigDecimal(0)) > 0){
            List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVoList.add(orderDerateVo);
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderInfoVo.setOrderDerateVoList(orderDerateVoList);
        }
    }
}
